package com.jsptpd.smartauth.common.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by LOG on 2017/11/3.
 */
public class CommonUtil {
    public static boolean isNullOrEmpty(String param) {
        if (null == param || "".equals(param)) {
            return true;
        }
        return false;
    }


    public static boolean isNullOrEmpty(String... params) {
        for (String param : params) {
            if (null == param || "".equals(param)) {
                return true;
            }
        }
        return false;
    }


    /**
     * @param xml      xml文
     * @param startTag 数组的项的开始标签
     * @param endTag   数组的项的结束标签
     * @param listTag  要加入的list标签包住N list
     * @return
     */
    public static String xmlListFormat(String xml, String startTag, String endTag, String listTag) {
        int firstListIndex = xml.indexOf(startTag);
        int endListIndex = xml.lastIndexOf(endTag);

        String result = xml.substring(0, firstListIndex);
        result += listTag;
        result += xml.substring(firstListIndex, endListIndex + endTag.length());

        listTag = "</" + listTag.substring(1);

        result += listTag;
        result += xml.substring(endListIndex + endTag.length());
        return result;
    }

    /**
     * map转大写
     *
     * @param map
     */
    public static Map mapHump(Map map) {
        Map newMap = new HashMap();
        Iterator<Map.Entry> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            String key = (String) entry.getKey();
            Object value = entry.getValue();

            String[] strs = key.split("_");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < strs.length; i++) {
                if (0 == i) {
                    sb.append(strs[i].toLowerCase());
                } else {
                    sb.append(strs[i].substring(0, 1));
                    sb.append(strs[i].substring(1).toLowerCase());
                }
            }
            String newKey = sb.toString();
            newMap.put(newKey, value);
        }
        return newMap;
    }


    /*
     * 打印异常堆栈
     */
    public static String printExceptionStackTrace(Exception e) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            //将出错的栈信息输出到printWriter中
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
        } finally {
            if (sw != null) {
                try {
                    sw.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (pw != null) {
                pw.close();
            }
        }
        return sw.toString();

    }

    public static Map convertFormData(Map mapData) {
        String page = (String) mapData.get("page");
        String rows = (String) mapData.get("rows");
        mapData.put("pageCopy", page);
        if ((page != null && !"".equals(page)) & (rows != null && !"".equals(rows))) {
            Integer intPage = Integer.valueOf(page);
            Integer intRows = Integer.valueOf(rows);
            int i = (intPage - 1) * intRows;
            mapData.put("page", i);
        }
        if (rows != null && !"".equals(rows))
            mapData.put("rows", Integer.valueOf(rows));
        String sort = (String) mapData.get("sort");
        if (sort != null) {
            sort = getSortString(sort);
            mapData.put("sort", sort);
        } else {
            mapData.put("sort", " order by OPERATE_TIME desc");
        }

        return mapData;
    }

    public static Map convertFormDataToDoList(Map mapData) {
        Integer pageNum = CommonUtil.String2Integer((String) mapData.get("pageNum"));
        Integer pageSize = CommonUtil.String2Integer((String) mapData.get("pageSize"));

        return mapData;
    }

    public static String getSysRef() {
//        int ref = UUID.randomUUID().hashCode();
//        return String.valueOf(ref);

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(currentTime);
//        int i = (int) ((Math.random() * 9 + 1) * 10);
        int i = (int) ((Math.random() * 9 + 1) * 10000000);
        String substring = dateString.substring(0, 8);
        String s1 = substring + i;
        System.out.println(s1);
        return s1;
    }


    private static String getSortString(String sort) {
        String[] sorts = sort.split(",");
        String s = "";
        for (int i = 0; i < sorts.length; i++) {
            String ss = sorts[i];
            if (ss.startsWith("-"))
                ss = ss.substring(1) + " desc ";
            else
                ss = ss + " asc ";
            s += "," + ss;
        }
        return " order by " + s.substring(1);
    }

    public static boolean vertifySQL(String str) {
        String inj_str = "exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare|;|or|-|+|," +
                "|%%|%_%";
        String[] inj_stra = inj_str.split("\\|");
        for (int i = 0; i < inj_stra.length; i++) {
            if (str.indexOf(inj_stra[i]) >= 0) {
                return false;
            }
        }
        return true;
    }

    public static Integer String2Integer(String str) {
        Integer value = null;
        try {
            value = Integer.valueOf(str);
        } catch (Exception e) {
        }
        return value;
    }
}
